// subPages/business/login/login.js
const app = getApp()
var util =  require('../../../utils/util.js');
var flag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!app.globalData.userInfo) {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      app.userInfoReadyCallback = userInfo => {
        if (userInfo != '') {
          this.setData({
            userInfo: userInfo
          });
        }
      }
    }else{
      this.setData({
        userInfo: app.globalData.userInfo
      })
    }
  },

  //授权登录
  unLoginBtn:function(){
    var that = this
    if(app.globalData.userInfo){
      that.setData({
        userInfo: app.globalData.userInfo
      })
    }else{
      let isLogin = app.globalData.userId?true:false 
      //未授权
      wx.navigateTo({
        url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ isLogin,
      })
    }
  },

  //获得商户类别
  getShopType:function(content){
    var type = ''
    if(content){
      if(content.health_shop_id){
        type = '1'
      }else if(content.community_service_shop_id){
        type = '0'
      }else if(content.kitchen_id){
        type = '2'
      }else{
        type = '3'
      }
    }
    return type
  },

  //获取id
  getShopId:function(){
    var id = ''
    switch (app.globalData.shopType) {
      case '1':
        id = app.globalData.communityContent.health_shop_id
        break;
      case '0':
        id = app.globalData.communityContent.community_service_shop_id        
        break;
      case '2':
        id = app.globalData.communityContent.kitchen_id        
        break;
      case '3':
        id = app.globalData.communityContent.community_id        
        break;
    }
    return id
  },

  //登录
  formSubmit:function(e){
    if(flag){
      return
    }
    flag = true
    if(app.globalData.userId){
      var account = e.detail.value.account
      var password = e.detail.value.password
      // account = 'ceshi2',
      // password = '123456'
  
      // account = '234',
      // password = '234'
  
      // account = 'alasun02',
      // password = '123456'
  
      // account = '123456',
      // password = '123456'
      if(account == '' || password == ''){
        util.showModal('提示','请填写账号密码')
        flag = false
      }
      this.save(account,password)
    }else{
      flag = false
      util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
        if (app.globalData.userInfo) {
          //已授权
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }else{
          // 在没有 open-type=getUserInfo 版本的兼容处理
          if(app.globalData.isAuthorized){
            app.userInfoReadyCallback = res => {
              if (res) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                //未授权
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          }else{
            wx.navigateTo({
              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
            })
          }
        }
      })
    }
    
  },

  //登录
  save:function(account,password){
    var that = this
    util.getAjax(app.globalData.apiUrl +'/Api/Alladmin/login_admin',function(res){
      console.log(res)
      if(res.data.status == 1){
        util.showToast('登录成功','success',2000)
        app.globalData.communityContent = res.data.content
        app.globalData.shopType = that.getShopType(res.data.content)
        app.globalData.shopId = that.getShopId()
        that.setData({
          account: '',
          password: ''
        })
        wx.navigateTo({
          url: '../index/index'
        })
      }else{
        util.showToast('登录失败，请重试','none',2000)
      }
      flag = false
    },{user_name: account,user_pwd: password})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})